package com.tfj.widget.sliding;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hyphenate.tfj.R;
import com.tfj.mvp.tfj.msg.bean.ItemMessage;
import com.tfj.utils.SysUtils;
import com.tfj.widget.GifTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 清单列表adapter
 * <p>
 * Created by xiaoqiang on 2018/5/30.
 */

public class SlidingAdapter extends SlidingBaseRecyclerViewAdapter<ItemMessage> {

    private OnDeleteClickLister mDeleteClickListener;
    private OnReadClickLister mReadClickListener;
    private OnAddClickLister onAddClickLister;
    private OnMinusClickLister onMinusClickLister;
    private OnCheckClickLister onCheckClickLister;
    private OnOutClickClickLister onOutClickClickLister;
    private Context context;
    private List<ItemMessage> itemMessages;
    private String TAG = "SlidingAdapter";
    private Handler handler;

    public SlidingAdapter(Context context, List<ItemMessage> data) {
        super(context, data, R.layout.item_message_sliding);
        this.context = context;
        this.itemMessages = data;
        handler = new Handler();
    }

    public ItemMessage getData(int position) {
        return this.itemMessages.get(position);
    }

    public List<ItemMessage> getDataAll() {
        if (this.itemMessages == null) {
            return new ArrayList<ItemMessage>();
        }
        return this.itemMessages;
    }

    public void replaceData(List<ItemMessage> data) {
        // 不是同一个引用才清空列表
        if (data != itemMessages) {
            itemMessages.clear();
            itemMessages.addAll(data);
        }
        notifyDataSetChanged();
    }

    //{"fromUserInfo":"{\"gender\":\"0\",\"phone\":\"13401592054\",\"avatar\":\"http:\\/\\/ww.didigo.es\\/uploads\\/44\\/ec\\/44ec79303732049f36dd5c5d7c9d0bb2673279_s.jpg\",\"qrcode\":\"http:\\/\\/ww.didigo.es\\/uploads\\/qrcode\\/user\\/66.png\",\"age\":\"0\",\"phoneNum\":\"13401592054\",\"name\":\"紫菜5bfe881992665\",\"genderText\":\"\"}"}
    @Override
    protected void onBindData(SlidingRecyclerViewHolder holder, ItemMessage bean, int position) {
        View view_delete = holder.getView(R.id.tv_delete);
        ImageView imageview_left = (ImageView) holder.getView(R.id.imageview_left);
        Glide.with(context).load(R.drawable.arrow_down).into(imageview_left);
        ((TextView) holder.getView(R.id.textView_name)).setText(bean.getFromUserName());
        View view_line = holder.getView(R.id.view_line);
        if (position == itemMessages.size() - 1) {
            view_line.setVisibility(View.GONE);
        } else {
            view_line.setVisibility(View.VISIBLE);
        }
        View rl_out = holder.getView(R.id.rl_out);
        rl_out.setTag(position);
        view_delete.setTag(position);
        if (!view_delete.hasOnClickListeners()) {
            view_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDeleteClickListener != null) {
                        mDeleteClickListener.onDeleteClick(v, (Integer) v.getTag());
                    }
                }
            });
        }
        if (!rl_out.hasOnClickListeners()) {
            rl_out.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onOutClickClickLister != null) {
                        onOutClickClickLister.onOutClickClick(v, (Integer) v.getTag());
                    }
                }
            });
            Log.i(TAG, "没有点击事件");
        } else {
            Log.i(TAG, "有点击事件了");
        }
        if (!TextUtils.isEmpty(bean.getMessageContent())) {
            if (bean.getContentType().equals("IMAGE")) {
                ((GifTextView) holder.getView(R.id.textview_bottom)).setText("[图片消息]");
            } else {
                ((GifTextView) holder.getView(R.id.textview_bottom)).setSpanText(handler, bean.getMessageContent(), false);
                ;
            }
        }

        if (!TextUtils.isEmpty(bean.getMsgTime())) {
            ((TextView) holder.getView(R.id.textview_right)).setText(SysUtils.LongFormatTime(bean.getMsgTime()));
        }

        if (bean.getUnreadMsgCount().equals("0")) {
            ((RelativeLayout) holder.getView(R.id.rl_circle)).setVisibility(View.GONE);
        } else if (Integer.parseInt(bean.getUnreadMsgCount()) < 100) {
            ((RelativeLayout) holder.getView(R.id.rl_circle)).setVisibility(View.VISIBLE);
            ((TextView) holder.getView(R.id.textview_count)).setText(bean.getUnreadMsgCount());
        } else {
            ((RelativeLayout) holder.getView(R.id.rl_circle)).setVisibility(View.VISIBLE);
            ((TextView) holder.getView(R.id.textview_count)).setText("99+");
        }
    }

    public void setOnOutClickListener(OnOutClickClickLister listener) {
        this.onOutClickClickLister = listener;
    }

    public void setOnDeleteClickListener(OnDeleteClickLister listener) {
        this.mDeleteClickListener = listener;
    }

    public void setOnReadClickListener(OnReadClickLister listener) {
        this.mReadClickListener = listener;
    }

    public void setOnAddClickListener(OnAddClickLister listener) {
        this.onAddClickLister = listener;
    }

    public void setOnMinusClickLister(OnMinusClickLister listener) {
        this.onMinusClickLister = listener;
    }

    public void setOnCheckClickLister(OnCheckClickLister listener) {
        this.onCheckClickLister = listener;
    }

    public interface OnOutClickClickLister {
        void onOutClickClick(View view, int position);
    }

    public interface OnDeleteClickLister {
        void onDeleteClick(View view, int position);
    }

    public interface OnReadClickLister {
        void onReadClick(View view, int position);
    }

    public interface OnAddClickLister {
        void onAddClick(View view, int position);
    }

    public interface OnMinusClickLister {
        void onMinusClick(View view, int position);
    }

    public interface OnCheckClickLister {
        void onCheckClick(View view, int position);
    }


}
